package com.zk.babala.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.zk.babala.BabalaApp;
import com.zk.babala.R;
import com.zk.babala.datasource.SourceInit;
import com.zk.babala.service.ISourceParse;
import com.zk.babala.util.HistoryUtil;
import com.zk.babala.util.LineUtil;
import com.zk.babala.util.RequestUtil;

import java.util.List;
import java.util.Map;


public class SettingActivity extends Activity {
    private Switch switchBootToLive;
    private Switch switchBootToApp;
    private EditText ipEditText;
    private Button generateIPButton;
    private Button clearIPButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_activity);
        setCurrentLineText();
        switchBootToLive = findViewById(R.id.switchBootToLive);
        switchBootToApp = findViewById(R.id.switchBootToApp);
        LinearLayout linerSourceLayout = findViewById(R.id.linerSourceList);
        List<ISourceParse> sourceList = new SourceInit().getSourceList();
        sourceList.forEach(p -> {
            Map<String, String> sourceMap = p.getSourceMeta();
            View item = LayoutInflater.from(this).inflate(R.layout.setting_item_activity, null);
            Button button = item.findViewById(R.id.lineItemButton);
            button.setText(sourceMap.get("name"));
            EditText text = item.findViewById(R.id.lineItemText);
            text.setText(sourceMap.get("url"));
            button.setOnClickListener(view -> {
                LineUtil.getLineUtil().setLine(sourceMap.get("source"), text.getText().toString());
                setCurrentLineText();
            });

            linerSourceLayout.addView(item);
        });


        findViewById(R.id.lineSetRefresh).setOnClickListener(view -> {
            BabalaApp.restartApp(this);
        });
        // 设置开关监听器
        switchBootToLive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                HistoryUtil.getHistoryInstance().saveStartToLive(isChecked);
            }
        });
        switchBootToLive.setChecked(HistoryUtil.getHistoryInstance().getStartToLive());
        switchBootToApp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                HistoryUtil.getHistoryInstance().saveBootToApp(isChecked);
            }
        });
        switchBootToApp.setChecked(HistoryUtil.getHistoryInstance().getBootToApp());

        ipEditText = findViewById(R.id.ip_edit_text);
        generateIPButton = findViewById(R.id.generate_ip_button);
        clearIPButton = findViewById(R.id.clear_ip_button);

        // 显示当前设置的IP
        ipEditText.setText(LineUtil.getLineUtil().getIp());
        // 点击按钮生成并显示随机IP
        generateIPButton.setOnClickListener(v -> {
            LineUtil.getLineUtil().setIp(RequestUtil.getRandomIP());
            ipEditText.setText(LineUtil.getLineUtil().getIp());
        });
        clearIPButton.setOnClickListener(v -> {
            LineUtil.getLineUtil().setIp("");
            ipEditText.setText(LineUtil.getLineUtil().getIp());
        });
    }

    private void setCurrentLineText() {
        TextView lineView = findViewById(R.id.currentLine);
        lineView.setText(String.format("当前线路：%s", LineUtil.getLineUtil().getLine()));
    }

}